1 using System.Collections;
2 using
System.Collections.Generic;
3 using
UnityEngine;
4 using
UnityEngine.UI;
5
6 public
class Cannon : MonoBehaviour {
7
8     
public GameObject cannonBullet;
9     
public Transform cannonTip;
10     
public Slider powerLevel;
11     
public AudioClip cannonShot;
12     
public int maxShot;
13     
public bool readyToShoot;
14     
public int shot;
15
16     
private int minLevel, maxLevel, timeDelay;
17     
private bool isMax, isCharging;
18     
private AudioSource audioSource;
19     
private float minRot, maxRot;
20     
private Vector3 currentRotation, touchPos;
21     
private int counter = 0;
22     
private Vector2 direction = Vector2.zero;
23
24     
void Awake(){
25         audioSource = GetComponent<AudioSource> ();
26     }
27
28     
// Use this for initialization
29     
void Start () {
30         readyToShoot =
true;
31         shot = maxShot;
32         timeDelay =
50;
33         minLevel =
0;
34         maxLevel =
50;
35         powerLevel.maxValue = maxLevel;
36         powerLevel.
value = minLevel;
37         maxRot =
20;
38         minRot = -
25;
39         Vector3 currentRotation = transform.rotation.eulerAngles;
40     }
41     
42     
// Update is called once per frame
43     
void Update () {
44         
if (GameplayController.instance.gameInProgress) {
45             
if (readyToShoot) {
46                 
if(Application.platform == RuntimePlatform.Android){
47                     TouchCannonShoot ();
48                 }
else if(Application.platform == RuntimePlatform.WindowsEditor){
49                     CannonShoot ();
50                 }
51
52             }
53
54             
if(Application.platform == RuntimePlatform.Android){
55                 TouchCannonMovement ();
56             }
else if(Application.platform == RuntimePlatform.WindowsEditor){
57                 CannonMovement ();
58             }
59                 
60         }
61     }
62
63     
void TouchCannonShoot(){
64         
if(Input.touchCount > 0){
65             Touch touch = Input.GetTouch (
0);
66
67             touchPos = Camera.main.ScreenToWorldPoint (touch.position);
68
69             Vector2 touchRayHit =
new Vector2 (touchPos.x, touchPos.y);
70
71             RaycastHit2D hit = Physics2D.Raycast (touchRayHit , Vector2.zero);
72
73             
if(hit.collider != null){
74                 
if(hit.collider.CompareTag("Player")){
75                     
if(touch.phase == TouchPhase.Stationary){
76                         
if (shot != 0) {
77                             UpdatePowerLevel ();
78                             isCharging =
true;
79                         }
80                     }
else if(touch.phase == TouchPhase.Ended){
81                         
if (shot != 0) {
82                             GameObject newCannonBullet = Instantiate (cannonBullet, cannonTip.position, Quaternion.identity)
as GameObject;
83                             newCannonBullet.transform.GetComponent<Rigidbody2D> ().AddForce (cannonTip.right * powerLevel.
value, ForceMode2D.Impulse);
84                             
if (GameController.instance != null && MusicController.instance != null) {
85                                 
if (GameController.instance.isMusicOn) {
86                                     audioSource.PlayOneShot (cannonShot);
87                                 }
88                             }
89                             shot--;
90                             powerLevel.
value = 0;
91                             readyToShoot =
false;
92                             isCharging =
false;
93                         }
94                     }
95                 }
96             }
97
98             
/*if(touch.phase == TouchPhase.Stationary){
99                 
if (shot != 0) {
100                     UpdatePowerLevel ();
101                 }
102             }
else if(touch.phase == TouchPhase.Ended){
103                 
if (shot != 0) {
104                     GameObject newCannonBullet = Instantiate (cannonBullet, cannonTip.position, Quaternion.identity)
as GameObject;
105                     newCannonBullet.transform.GetComponent<Rigidbody2D> ().AddForce (cannonTip.right * powerLevel.
value, ForceMode2D.Impulse);
106                     
if (GameController.instance != null && MusicController.instance != null) {
107                         
if (GameController.instance.isMusicOn) {
108                             audioSource.PlayOneShot (cannonShot);
109                         }
110                     }
111                     shot--;
112                     powerLevel.
value = 0;
113                     readyToShoot =
false;
114                 }
115             }*/

116             
117         }
118     }
119
120     
void CannonShoot(){
121         
if (Input.GetKey (KeyCode.Space)) {
122             
if(shot != 0){
123                 UpdatePowerLevel ();
124             }
125         }
else if(Input.GetKeyUp(KeyCode.Space)){
126             
if (shot != 0) {
127                 GameObject newCannonBullet = Instantiate (cannonBullet, cannonTip.position, Quaternion.identity)
as GameObject;
128                 newCannonBullet.transform.GetComponent<Rigidbody2D> ().AddForce (cannonTip.right * powerLevel.
value, ForceMode2D.Impulse);
129                 
if (GameController.instance != null && MusicController.instance != null) {
130                     
if (GameController.instance.isMusicOn) {
131                         audioSource.PlayOneShot (cannonShot);
132                     }
133                 }
134                 shot--;
135                 powerLevel.
value = 0;
136                 readyToShoot =
false;
137             }
138         }
139
140     }
141
142     
void UpdatePowerLevel(){
143         
if (!isMax) {
144             powerLevel.
value += timeDelay * Time.deltaTime;
145
146             
if(powerLevel.value == maxLevel){
147                 isMax =
true;
148             }
149
150         }
else {
151             powerLevel.
value -= timeDelay * Time.deltaTime;
152
153             
if(powerLevel.value == minLevel){
154                 isMax =
false;
155             }
156         }
157     }
158
159     
void CannonMovement(){
160
161         
if (Input.GetKey (KeyCode.UpArrow)) {
162             currentRotation.z +=
50f * Time.deltaTime;
163         }
else if (Input.GetKey (KeyCode.DownArrow)) {
164             currentRotation.z -=
50f * Time.deltaTime;
165         }
166             
167         currentRotation.z = Mathf.Clamp(currentRotation.z, minRot, maxRot);
168
169         transform.rotation = Quaternion.Euler (currentRotation);
170     }
171
172     
void TouchCannonMovement(){
173         
if(Input.touchCount > 0){
174             Touch touch = Input.GetTouch (
0);
175
176             
if (touch.position.x < 300 && !isCharging && touch.position.y > 160) {
177
178                 
if (touch.phase == TouchPhase.Moved) {
179                     Vector2 touchDeltaPosition = touch.deltaPosition;
180                     direction = touchDeltaPosition.normalized;
181
182                     
if (direction.y > 0 && touchDeltaPosition.x > -10 && touchDeltaPosition.x < 10) {
183                         currentRotation.z +=
50f * Time.deltaTime;
184                     }
else if (direction.y < 0 && touchDeltaPosition.x > -10 && touchDeltaPosition.x < 10) {
185                         currentRotation.z -=
50f * Time.deltaTime;
186                     }
187
188                 }
189             }
190         }
191
192         currentRotation.z = Mathf.Clamp(currentRotation.z, minRot, maxRot);
193
194         transform.rotation = Quaternion.Euler (currentRotation);
195             
196     }
197         
198 }


Use this for initialization

Update is called once per frame




trò chơi Cannon Siege miễn phí 12.308 lượt xem

Gõ tìm kiếm nhanh...